<template>
  <div>
    <div class="lable-top">
      <el-form label-position="top" size="mini">
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item v-permission="['super']" label="组织">
              <el-select v-model="search.org_id" filterable>
                <el-option
                  v-for="(item, index) in company"
                  :key="index"
                  :label="item.name"
                  popper-class="w100"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item v-permission="['super']" label=" ">
              <el-button @click="handleQueryOrgYaraBtn">
                查询规则
              </el-button>
              <el-button @click="handleQueryPublicYaraBtn">
                查询公共规则
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-row justify="space-between" type="flex">
            <el-col :span="10">
              <el-button @click="handleClickYaraDelete">规则删除</el-button>
              <el-button @click="handleClickYaraUnDisable">规则启用</el-button>
              <el-button
                style="margin-left: 0;"
                @click="handleClickYaraDisable"
              >
                规则禁用
              </el-button>
            </el-col>
            <el-col :span="3">
              <el-button
                style="width: 100%;"
                @click="$refs.ToastNewYara.show(search.org_id)"
              >
                创建新规则
              </el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <el-table
        class="mt10"
        :data="tableData"
        :header-cell-style="{ background: '#f5f5f5' }"
        size="mini"
      >
        <el-table-column label="选择" width="60">
          <template slot-scope="scope">
            <el-radio
              v-model="tableSelect"
              :label="scope.row.id"
              style="margin-left: 10px;"
              @change="getTemplateRow(scope.row.id)"
            >
              &nbsp;
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column label="规则 ID" prop="id" sortable />
        <el-table-column label="规则名" prop="name" sortable />
        <el-table-column label="创建者" prop="user_email" sortable />
        <el-table-column label="创建时间" prop="create_time" sortable />
        <el-table-column label="状态" prop="disabled" sortable />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              class="f12"
              type="text"
              @click="
                handleClickYaraEdit(scope.row, scope.row.id, scope.row.abstract)
              "
            >
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt10 el-row--flex is-justify-end">
        <el-pagination
          background
          :current-page="page"
          layout="total, prev, pager, next"
          :page-size="limit"
          :page-sizes="[100, 200, 300, 400]"
          small
          :total="count"
          @current-change="handleCurrentChangePage"
        />
      </div>
    </div>
    <toast-new-yara ref="ToastNewYara" @change="onLoad" />
    <edit-yara ref="EditYara" @change="onLoad" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";

const blockName = block => {
  const dict = {
    0: "否",
    1: "是"
  };
  return dict[Number(block)];
};
const disabledName = disabled => {
  const dict = {
    2: "待审核",
    1: "已禁用",
    0: "生效中"
  };
  return dict[Number(disabled)];
};

// import JsonViewer from "vue-json-viewer";
import ToastNewYara from "@/views/antivirus/toast/newYara";
import EditYara from "./toast/editYara.vue";
import org_getall from "@/utils/mixin/org_getall";
import antivirus from "@/api/antivirus";
import radioTable from "@/utils/mixin/radioTable";
import checkSelectTable from "@/utils/mixin/checkSelectTable";
export default {
  name: "RuleManage",
  components: { ToastNewYara, EditYara },
  mixins: [org_getall, radioTable, checkSelectTable],
  data: () => {
    return {
      tableData: [],
      yaraType: "handleQueryOrgYara",
      search: {
        org_id: "0"
      },
      limit: 20,
      page: 1,
      count: 0,
      desc: {
        abstract: "",
        yaraDesc: ""
      }
    };
  },
  computed: mapGetters(["UserInfo"]),

  mounted() {
    this.onLoad();
    if (this.UserInfo.org_id) this.search.org_id = this.UserInfo.org_id;
  },
  methods: {
    async onLoad() {
      await this.loadingOrgGetall();
      await this.handleQueryOrgYara();
    },
    // async handleClickYaraDetail(yara_id, abstract) {
    //   this.$refs.ToastNewYara.show(this.search.org_id);
    //   let yaraDesc = await antivirus.detail({ yara_id }).then(res => {
    //     return res.data.yara;
    //   });
    //   this.$set(this, "desc", {
    //     abstract,
    //     yaraDesc
    //   });
    // },
    async handleClickYaraEdit(yara) {
      let yaraDesc = await antivirus.detail({ yara_id: yara.id }).then(res => {
        return res.data.yara;
      });
      yara.yara = yaraDesc;
      this.$refs.EditYara.show(yara);
    },
    handleClickYaraDisable() {
      let yaraData = {};
      this.is_check_table("id") &&
        (yaraData = this.is_get_select_data()) &&
        this.$confirm("您确定禁用此规则吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return antivirus.disable({
              yara_id: yaraData.id
            });
          })
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: res.data.message,
                type: "success"
              });
              this[this.yaraType]();
            } else {
              this.$message({
                message: res.data.message,
                type: "warning"
              });
            }
          });
    },
    handleClickYaraUnDisable() {
      let yaraData = {};
      this.is_check_table("id") &&
        (yaraData = this.is_get_select_data()) &&
        this.$confirm("您确定启用此规则吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return antivirus.undisable({
              yara_id: yaraData.id
            });
          })
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: res.data.message,
                type: "success"
              });
              this[this.yaraType]();
            } else {
              this.$message({
                message: res.data.message,
                type: "warning"
              });
            }
          });
    },
    handleClickYaraDelete() {
      let yaraData = {};
      this.is_check_table("id") &&
        (yaraData = this.is_get_select_data()) &&
        this.$confirm("您确定删除此规则吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return antivirus._delete({
              yara_id: yaraData.id
            });
          })
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: res.data.message,
                type: "success"
              });
              this.page = 1;
              this[this.yaraType]();
            } else {
              this.$message({
                message: res.data.message,
                type: "warning"
              });
            }
          });
    },
    async handleQueryPublicYara() {
      this.yaraType = "handleQueryPublicYara";
      const { count, yaras } = await antivirus
        .getpublic({
          limit: this.limit,
          page: this.page
        })
        .then(res => {
          res.data.yaras.map(k => {
            k.block = blockName(k.block);
            k.disabled = disabledName(k.disabled);
            return k;
          });
          return res.data;
        });
      this.count = count;
      this.tableData = yaras;
    },
    async handleQueryOrgYara() {
      this.yaraType = "handleQueryOrgYara";
      const { count, yaras } = await antivirus
        .getorg({
          ...this.search,
          limit: this.limit,
          page: this.page
        })
        .then(res => {
          res.data.yaras.map(k => {
            k.block = blockName(k.block);
            k.disabled = disabledName(k.disabled);
            return k;
          });
          return res.data;
        });
      this.count = count;
      this.tableData = yaras;
    },
    handleQueryPublicYaraBtn() {
      this.page = 1;
      this.handleQueryPublicYara();
    },
    handleQueryOrgYaraBtn() {
      this.page = 1;
      this.handleQueryOrgYara();
    },
    getData() {},
    handleCurrentChangePage(val) {
      this.page = val;
      this[this.yaraType]();
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .jv-container .jv-code.boxed {
  max-height: 50px;
  overflow: auto;
}
</style>
